<?php
use yii\helpers\Html;
use \backend\controllers\DtActionFilter;
$project_id = Yii::$app->request->get("project_id");

if (!empty($project_id)){
    $isView = DtActionFilter::checkProject('project_edit',$project_id);
}else{
    $isView=true;
    return '无用户id';
}
?>
<div class="container containerShadow">
    <div class="row ">
        <div class="topimg">
            <div class="left pull-left">
                <span>基本信息</span>
            </div>
            <div class="pull-right">
                <a href="javascript:;">
                    <?php
                        if ($isView){
                            echo Html::img("/image/projectDetails/edit.png",["id"=>"getBase","data-val"=>1]);
                        }
                    ?>
                </a>
            </div>
        </div>
        <form action="" class="baseW" >
            <table class='tableRaius' border="0" cellspacing="0" cellpadding="0" style="border-collapse:separate;border-spacing: 0;">

                <tr>
                    <td>项目名称</td>
                    <td colspan="3" class="one-txt-cut">
                    	<pre><?= !empty($Base['title'])?$Base['title']:'' ?></pre>
                    </td>
                    <td>项目编号</td>
                    <td>
                    	<span class="one-txt-cut">
                    		 <input type="text" readonly="readonly" name="" id="" title="<?= !empty($Base['code'])?$Base['code']:'' ?>" value="<?= !empty($Base['code'])?$Base['code']:'' ?>" />
                    	</span>
                       
                    </td>
                </tr>
                <tr>
                    <td>项目类型</td>
                    <td colspan="3">
                        <input type="text" readonly="readonly" name="" id="" title="<?= !empty($Base['type2'])?Yii::$app->params['projectCategory'][$Base['type2']]['name']:'';?>" value="<?= !empty($Base['type2'])?Yii::$app->params['projectCategory'][$Base['type2']]['name']:'';?>" />
                    </td>
                    <td>省份地区</td>
                    <td>
                    	<pre><?= !empty($Base['provinceName'])?$Base['provinceName']:'' ?><?= !empty($Base['cityName'])?$Base['cityName'].'市':'' ?></pre>
                    </td>
                </tr>
                <tr>
                    <td>项目状态</td>
                    <td colspan="3">
                        <?php
                        $stateName = '';
                        if (!empty($state)){
                            if ($state==4 ||$state==5){
                                $stateName = Yii::$app->params['package_state'][$state];
                            }
                        }
                        ?>
                        <input type="text" readonly="readonly" class="editNO" id="status" value="<?= $stateName ?>"/>
                    </td>
                    <td>诉讼状态</td>
                    <td>
                        <input type="text" readonly="readonly" name="" id="" title="" value="<?=$Base['is_suit']>-2?Yii::$app->params['isSuit'][$Base['is_suit']]['name']:'';?>" />
                    </td>
                </tr>
                <tr>
                    <td>企业类型</td>
                    <td colspan="3">
                    	
                        <?php
                        $enterpriseVal = !empty($Base['enterprise']) ? array_filter(explode(',',$Base['enterprise'])) :[];
                        $enterprises = Yii::$app->params['enterprises'];
                        $enterpriseStr = '';
                        foreach ($enterprises as $k => $v){
                            $name = $v['name'];
                            $val  = $v['val'];
                            $for  = $v['for'];
                            if (in_array($val,$enterpriseVal)){
                                $enterpriseStr = $name;
                            }
                        }
                        ?>
                        <pre><?= $enterpriseStr?></pre>
                    </td>
                    <td>所属行业</td>
                    <td>
                        <?php
                        $industryVal = !empty($Base['industry']) ? array_filter(explode(',',$Base['industry'])) :'';
                        $industrys = Yii::$app->params['industrys'];
                        $industryStr = '';
                        foreach ($industrys as $k => $v){
                            $name = $v['name'];
                            $val  = $v['val'];
                            $for  = $v['for'];
                            if (!empty($industryVal) && in_array($val,$industryVal)){
                                $industryStr = $industryStr.'、'.$name;
                            }
                        }
                        if (!empty($industryStr)){
                            $industryStr = mb_substr($industryStr,1,strlen($industryStr));
                        }
                        ?>
                        <pre><?= $industryStr ?></pre>
                    </td>
                </tr>
                <tr>
                    <td>借款人名称</td>
                    <td colspan="3">
                    	<pre><?=!empty($Base['borrower'])?$Base['borrower']:''?></pre>
                    </td>
                    <td>借款人状况</td>
                    <td>
                        <?php
                        $borrowerStatesSelected = !empty($Base['management_status']) ? intval($Base['management_status']) :'';
                        $borrowerStates = Yii::$app->params['borrowerStates'];
                        $htmlStr = '';
                        foreach ($borrowerStates as $k => $v){
                            $name = $v['name'];
                            $val  = $v['val'];
                            if ($v['val'] == $borrowerStatesSelected){
                                $htmlStr = $name;
                            }
                        }
                        ?>
                        <pre><?= $htmlStr?></pre>
                    </td>
                </tr>

                <tr class="benJin">
                    <td>本金（元）</td>
                    <td>
                        <input type="text" readonly="readonly" name="" id="" title="" value="<?= !empty($Base['principal'])?\common\helps\formatMoney::format_money($Base['principal'],2):'';?>" />
                    </td>
                    <td>利息（元）</td>
                    <td>
                        <input type="text" readonly="readonly" name="" id="" title="" value="<?= !empty($Base['interest'])?\common\helps\formatMoney::format_money($Base['interest']):'';?>" />
                    </td>
                    <td>本息合计</td>
                    <td>
                        <input type="text" readonly="readonly" name="" id="" title="" value="<?= \common\helps\formatMoney::format_money($Base['principal']+$Base['interest'])?>" />
                    </td>
                </tr>
                <tr>
                    <td>保证人/抵押人</td>
                    <td colspan="5">
                        <pre><?= !empty($Base['guarantor'])?$Base['guarantor']:'';?></pre>
                    </td>
                </tr>
                <tr>
                    <td>保证人/抵押人状况</td>
                    <td colspan="5">
                        <pre><?= !empty($Base['guarantor_status'])?$Base['guarantor_status']:'';?></pre>
                    </td>
                </tr>
                 <tr>
                    <td>担保方式</td>
                    <td colspan="5">
                        <pre><?= !empty($Base['warranty'])?$Base['warranty']:'';?></pre>
                    </td>
                </tr>
                <tr>
                    <td>抵押物详情描述</td>
                    <td colspan="5">
                    	<pre><?= !empty($Base['mortgage_details'])?$Base['mortgage_details']:'';?></pre>
                       <!-- <input type="text" readonly="readonly" name="" id="" title="<?= !empty($Base['mortgage_details'])?$Base['mortgage_details']:'';?>" value="<?= !empty($Base['mortgage_details'])?$Base['mortgage_details']:'';?>" />-->
                    </td>
                </tr>
            </table>

        </form>

        <form action="" id="jiben" class="baseEdit borderColor" style="display: none;">
            <table class='tableRaius editTable tableEdit1' border="0" cellspacing="0" cellpadding="0" style="border-collapse:separate;border-spacing: 0;">
                <tr style="width: 100%!important;">
                    <td style="width: 15%!important;">项目名称</td>
                    <td colspan="3" style="width: 35%;">
                        <input type="text" name="" id="title" value="<?= !empty($Base['title'])?$Base['title']:'' ?>" />
                    </td>
                    <td class="text-center" style="width: 15%!important;">项目编号</td>
                    <td style="width: 35%!important;">
                        <input type="text" readonly="readonly" class="editNO" name="" id="code" value="<?= !empty($Base['code'])?$Base['code']:'' ?>" />
                    </td>
                </tr>
                <tr>
                    <td style="width: 15%!important;">项目类型</td>
                    <td colspan="3" style="width: 35%!important;">
                        <select name="" id="projeactType">
                            <option value="" <?= $Base['type2']==0?'selected':''?> data-val="0">请选择</option>
                            <?php
                            $projectCategory = Yii::$app->params['projectCategory'];
                            foreach ($projectCategory as $k => $v){
                                if ($k == $Base['type2']){
                                    echo"<option value='".$v['val']."' selected='selected'>".$v['name']."</option>";
                                }else{
                                    echo"<option value='".$v['val']."'>".$v['name']."</option>";
                                }
                            }
                            ?>
                        </select>
                    </td>
                    <td class="text-center" style="width: 15%!important;">省份地区</td>
                    <?php
                    $provinceID = $Base['province'];
                    ?>
                    <td style="width: 35%!important;">
	                    <select name="" class="pull-left shengSelect"  id="province" >
	                        <option  value='0' <?= $provinceID==0?'selected':''?>>请选择</option>";
	                        <?php
	                        foreach ($provinceData as $k => $v) {
	                            if ($provinceID==($k+1) && $provinceID!=0){
	                                echo "<option selected data-id=".$v['id']." value=".$v['id'].">".$v['title']."</option>";
	                            }else{
	                                echo "<option  data-id=".$v['id']." value=".$v['id'].">".$v['title']."</option>";
	                            }
	                        }
	                        ?>
	                    </select>
	                    <span class="selectText select_province">省</span>
	                     <span class="selectText pull-right select_province">市</span>
	                     <select name="" class="pull-right shiSelect"  id="city">
	                    </select>
                    <td>
                </tr>
                <tr>
                    <td style="width: 15%!important;">项目状态</td>
                    <td colspan="3" style="width: 35%!important;">
                        <?php
                        $stateName = '';
                            if (!empty($state)){
                                if ($state==4 ||$state==5){
                                    $stateName = Yii::$app->params['package_state'][$state];
                                }
                            }
                        ?>
                        <input type="text" readonly="readonly" class="editNO" id="status" value="<?= $stateName ?>"/>
                    </td>
                    <td class="text-center" style="width: 15%!important;">诉讼状态</td>
                    <td style="width: 35%!important;">
                        <select name="" id="is_suit">
                            <?php
                            $isSuit = Yii::$app->params['isSuit'];
                            foreach ($isSuit as $k => $v){
                                if ($k!=-2){
                                    $is_suit = isset($Base['is_suit'])&&$Base['is_suit']==$k?'selected':'';
                                    $name = $v['name'];
                                    echo <<<ISSUIT
                            <option data-val="$k" $is_suit >$name</option>
ISSUIT;
                                }
                            }
                            ?>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>所属行业</td>
                    <td colspan="5" class="hangye" id="industry">
                        <?php
                        $industryVal = !empty($Base['industry']) ? array_filter(explode(',',$Base['industry'])) :'';
                        $industrys = Yii::$app->params['industrys'];
                        foreach ($industrys as $k => $v){
                            $name = $v['name'];
                            $val  = $v['val'];
                            $for  = $v['for'];
                            if (!empty($industryVal) && in_array($val,$industryVal)){
                                echo "<label for='".$for."' data-val=".$val."  class='labelActive'>".$name."</label><input type='checkbox' id='".$for."'/>";
                            }else{
                                echo "<label for='".$for."' data-val=".$val." >".$name."</label><input type='checkbox' id='".$for."'/>";
                            }
                        }
                        ?>
                    </td>
                </tr>
                <tr class="hangye ">
                    <td>企业类型</td>
                    <td colspan="5" class="type" id="enterpriseS">
                        <?php
                        $enterpriseVal = !empty($Base['enterprise']) ? array_filter(explode(',',$Base['enterprise'])) :'';
                        $enterprises = Yii::$app->params['enterprises'];
                        foreach ($enterprises as $k => $v){
                            $name = $v['name'];
                            $val  = $v['val'];
                            $for  = $v['for'];
                            if (in_array($val,$enterpriseVal)){
                                echo "<label for='".$for."' data-val=".$val."  class='labelActive'>".$name."</label><input type='radio' value='' name='enterpriseType' id='".$for."'/>";
                            }else{
                                echo "<label for='".$for."' data-val=".$val." >".$name."</label><input type='radio' value='' name='enterpriseType' id='".$for."'/>";
                            }
                        }
                        ?>
                    </td>
                </tr>
                <tr>
                    <td>借款人名称</td>
                    <td colspan="3">
                        <input type="text" name="" id="borrowerName" value="<?=!empty($Base['borrower'])?$Base['borrower']:''?>" />
                    </td>
                    <td class="text-center">借款人状况</td>
                    <td>
                        <select name="" id="borrowerStatus">
                            <?php
                            $borrowerStatesSelected = !empty($Base['management_status']) ? intval($Base['management_status']) :'';
                            $borrowerStates = Yii::$app->params['borrowerStates'];
                            foreach ($borrowerStates as $k => $v){
                                $name = $v['name'];
                                $val  = $v['val'];
                                if ($v['val'] == $borrowerStatesSelected){
                                    echo "<option value='".$val."' selected>".$name."</option>";
                                }else{
                                    echo "<option value='".$val."'>".$name."</option>";
                                }
                            }
                            ?>
                        </select>
                    </td>
                </tr>

                <tr class="benJin">
                    <td>本金（元）</td>
                    <td class="yz">
                    	<input isNumber='true' id="principal" onkeydown="true"  type="text"   value="<?= !empty($Base['principal'])?$Base['principal']:'';?>" name="principal" />
                        <!--<input isNumber='true'id="principal" onkeydown="true" type="text" onkeyup="this.value=this.value.replace(/[^\d{1,}\.\d{1,}|\d{1,}]/g,'')" onafterpaste="this.value=this.value.replace(/[^\d{1,}\.\d{1,}|\d{1,}]/g,'')"  value="<?= !empty($Base['principal'])?$Base['principal']:'';?>" name="principal" />-->
                    </td>
                    </td>
                    <td class="lixi">利息（元）</td>
                    <td class="yz">
                    	 <input type="text" id="interest" isNumber='true' onkeydown="true"   value="<?= !empty($Base['interest'])?$Base['interest']:'';?>"   onkeydown="true" />
                       <!-- <input type="text" id="interest" isNumber='true' onkeydown="true"   onkeyup="this.value=this.value.replace(/[^\d{1,}\.\d{1,}|\d{1,}]/g,'')" onafterpaste="this.value=this.value.replace(/[^\d{1,}\.\d{1,}|\d{1,}]/g,'')"  value="<?= !empty($Base['interest'])?$Base['interest']:'';?>"  number onkeydown="true" />-->
                    </td>
                    <td class="text-center">本息合计</td>
                    <td class="yz">
                        <input type="text" id="benxi" isNumber='true' disabled onkeydown="true" number onkeydown="true" value="<?=$Base['principal']+$Base['interest'] ?>"/>
                    </td>
                </tr>

                <tr>
                    <td>保证人/抵押人</td>
                    <td colspan="5">
                        <textarea name="" rows="" cols="" class="textarea-edit" id="guarantor"><?= !empty($Base['guarantor'])?$Base['guarantor']:'';?></textarea>
                    </td>
                </tr>
                <tr>
                <td class="">保证人/抵押人状况</td>
                    <td colspan="5">
                        <textarea name="" rows="" cols="" class="textarea-edit" id="guarantorState"><?= !empty($Base['guarantor_status'])?$Base['guarantor_status']:'';?></textarea>
                    </td>
                </tr>
                 <td class="">担保方式</td>
                    <td colspan="5">
                        <textarea name="" rows="" cols="" class="textarea-edit" id="warranty"><?= !empty($Base['warranty'])?$Base['warranty']:'';?></textarea>
                    </td>
                </tr>

                <tr>
                    <td>抵押物详情描述</td>
                    <td colspan="5">
                        <textarea name="" rows="" cols="" class="textarea-edit" id="mortgage_details"><?= !empty($Base['mortgage_details'])?$Base['mortgage_details']:'';?></textarea>
                    </td>
                </tr>
            </table>
            <div class="cancelInConfirm clearfix">
                <input id="ajaxBase" class="cancelInConfirmActive" type="button" value="保存" />
                <input id='cancel' type="button" value="取消" />
            </div>
        </form>

    </div>
</div>
<!-- textarea 高度设置对比div -->
	<div style="max-height: 1px; overflow: hidden;">
		<div id="d1" style="visibility:hidden;min-height: 80px;"></div>
	</div>
<script>
    $(function () {
        var title    = ''; //项目名称
        var code    = ''; //项目编号
        var projeactType = ''; //项目类型
        var province = '';   //省份
        var city = '';   //省份
        var enterprise = ''; //企业类型
        var borrowerName = '';//借款人名称
        var borrowerStatus = ''; //借款人状况
        var guarantor = ''; //保证人/抵押人
        var guarantorState = ''; //保证人/抵押人状态
        var warranty = ''; //担保方式
        var principal = ''; //本金（元）
        var interest = ''; //利息（元）
        var benxi = ''; //本息和（元）
        var mortgage_details = '';
        //按键获取想加的总和
        $("#principal,#interest").keyup(function () {
            principal = $("#principal").val()=='' ? 0 :$("#principal").val();
            interest = $("#interest").val()==''?0:$("#interest").val();
            sumMoney = parseFloat(interest) + parseFloat(principal);
            $("#benxi").val(sumMoney)
        })
        //ajax 保存数据
        $('#ajaxBase').click(function(){
            var industry = ''; //所属行业
            var is_suit = ''; //诉讼状态
            //--------------------------项目名称/项目编号/项目类型/省份/借款人名称/借款人状况/保证人/抵押人/保证人/抵押人状态/本金（元）/利息（元）/本息和（元）/亮点明细---start----------------
            title = $("#title").val();
            code = $("#code").val();
            projeactType = $("#projeactType option:selected").val();
            province = $("#province option:selected").val();
            city = $("#city option:selected").val();
            borrowerName = $("#borrowerName").val();
            borrowerStatus = $("#borrowerStatus option:selected").val();
            guarantor = $("#guarantor").val();
            guarantorState = $("#guarantorState").val();
            warranty = $("#warranty").val();
            principal = $("#principal").val();
            interest = $("#interest").val();
            benxi = $("#benxi").val();
            mortgage_details = $("#mortgage_details").val();
            //--------------------------项目名称/项目编号/项目类型/省份------end-------------
            //--------------------------获取所属行业的值---start----------------
            $('#industry').find('label').each(function() {
                if($(this).attr('class') == 'labelActive'){
                    industry+=$(this).data('val')+',';
                }
            })
            if (industry.length > 0){
                industry = industry.substr(0,industry.length-1)
            }
            //--------------------------获取所属行业的值---end----------------
            //--------------------------企业类型---start----------------
            $('#enterpriseS').find('label').each(function() {
                if($(this).attr('class') == 'labelActive'){
                    enterprise = $(this).data('val');
                }
            })
            //--------------------------企业类型---end----------------

            //--------------------------诉讼状态---start----------------
            is_suit =  $("#is_suit option:selected").data('val');
            //--------------------------诉讼状态---end----------------
            //--------------------------post数据---start----------------
            var data={
                'industry':industry,
                'title':title,
                'code':code,
                'projeactType':projeactType,
                'province':province,
                'city':city,
                'enterprise':enterprise,
                'borrowerName':borrowerName,
                'borrowerStatus':borrowerStatus,
                'guarantor':guarantor,
                'guarantorState':guarantorState,
                'warranty':warranty,
                'principal':principal,
                'interest':interest,
                'benxi':benxi,
                'is_suit':is_suit,
                'mortgage_details':mortgage_details,
                'project_id':project_id,
                'package_id':package_id,
            };
            $.post('/project/ajax-save-base-project',{'data':data},function (res) {
                if (res){
                    layer.msg("更新成功");
                    window.location.reload();
//                    baseMW();
                }else{
                    layer.msg("更新失败");
                }
            })
            //--------------------------post数据---end----------------
        })
        //-----------------渲染无编辑页面--------------------
        function baseMW() {
            $.get('/project/get-mortgage-view',{'project_id':project_id,'postion':1},function (res) {
                $(".jiBenXinXiDOM").children().remove();
                $(".jiBenXinXiDOM").html(res);
            })
        }
        //ajax 调用 二级城市
        $("#province").change(function () {
            var provinceId = $(this).val();
            if (provinceId !=0 ){
                $.get('/search-package/ajax-city',{'provinceID':provinceId},function (res) {
                    var resData = JSON.parse( res );
                    if (resData.code==200){
                        rendering(resData.data.res);
                    }else{
                        alert("错误")
                    }
                })
            }else{
                var cityHtml0="<option value='' selected=''>请选择</option>";
                $("#city").html("");
                $("#city").append(cityHtml0);
            }
        })
        //刷新页面加载城市数据
        var getProvinceID = '<?=  $Base['province'] ?>';
        $().ready(function(){
            if (getProvinceID != 0){
                $.get('/search-package/ajax-city',{'provinceID':getProvinceID},function (res) {
                    var resData = JSON.parse( res );
                    if (resData.code==200){
                        rendering(resData.data.res);
                    }else{
                        alert("错误")
                    }
                })
            }
        })
        var cityID = '<?=  $Base['city'] ?>';
        function rendering(res) {
            var cityHtml = "";
            $(res).each(function(i,e){
                var title = e.title;
                var id    = e.id;
                if (cityID==id){
                    cityHtml+= "<option selected value='"+id+"'>"+title+"</option>";
                }else{
                    cityHtml+= "<option value='"+id+"'>"+title+"</option>";
                }
            });
            cityHtml="<option value='' selected=''>请选择</option>"+cityHtml;
            $("#city").html("");
            $("#city").append(cityHtml);
        }
        $("#cancel").click(function () {
            $.post('/project/ajax-un-lock',{'postion':2,'project_id':project_id},function (res) {
                var JSONData = JSON.parse(res)
                if (JSONData.code == 200){
                    layer.msg(JSONData.message);
                }else{
                    layer.msg(JSONData.message);
                }
            });
            $("#jiBenXinXiEdit").hide();
            $(".jiBenXinXiW").show();
        })


        $('#city').select2({
            language: "zh-CN",  //设置 提示语言
            width: '160px', //设置下拉框的宽度
            tokenSeparators: [',', ' '],
            placeholder: "请选择城市",  //提示语
            maximumSelectionLength :30,   //限制搜索的个数
            tags: false                //可以手动添加，若限制手动添加，设置为false
        })

        $('#province').select2({
            language: "zh-CN",  //设置 提示语言
            width: '160px', //设置下拉框的宽度
            tokenSeparators: [',', ' '],
            placeholder: "请选择省",  //提示语
            maximumSelectionLength :30,   //限制搜索的个数
            tags: false                //可以手动添加，若限制手动添加，设置为false
        })
    })

    //所属行业
    $('.jiBenXinXi .labelActiveBox').on('click', 'label', function() {
        $(this).toggleClass('labelActive');
        // 获取当前label对应input的值
        //		var str = $(this).attr('for')
    })
    //基本信息中的企业类型

    $('.enterpriseType input[name="enterpriseType"]').on('click', function() {
        if($(this).is(':checked')) {
            $($(this)).parent().addClass('typeActive').siblings().removeClass('typeActive');
        }
    })
    //诉讼状态
    $('.suSongZhuangTai strong').on('click', 'label', function() {
        // 获取当前label对应input的值
        var str = $(this).attr('for')
        $(this).children('i').addClass('radioActive');
        $(this).siblings('label').children('i').removeClass('radioActive');
    })
    
	// textarea 高度展示问题
    $(".textarea-edit").keyup(function(){
      var val = $(this).val();
      $("#d1").html("<pre>" +val+" "+"</pre>");
      var h = $("#d1").height() ;
      $(this).parent().height(h).end().height(h);
    });
    
    
</script>